//
//  SheZhi.m
//  
//
//  Created by dllo on 16/1/30.
//
//

#import "SheZhi.h"
#import "PCH.pch"
#import "GuanYuWoMen.h"
#import "YeJianMoShi.h"
#import <DKNightVersion.h>
#import <UIImageView+WebCache.h>

#import <Social/Social.h>

@interface SheZhi () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)UIButton *buttonOfLeft;
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSArray *array;
@property (nonatomic, retain)NSArray *arr;
@property (nonatomic, assign)CGFloat h;

@end

@implementation SheZhi

- (void)dealloc {
    self.tableView.delegate = nil;
    self.tableView.dataSource = nil;
    [_array release];
    [_tableView release];
    [_buttonOfLeft release];
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"设置";
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:19], NSFontAttributeName, [UIColor whiteColor], NSForegroundColorAttributeName,nil];

    
    
    self.tabBarController.tabBar.hidden = YES;
    
    /** 主页navigation左侧button */
    self.buttonOfLeft = [UIButton buttonWithType:UIButtonTypeCustom];
    self.buttonOfLeft.frame = CGRectMake(5, 10, 30, 30);
    [self.buttonOfLeft addTarget:self action:@selector(fanHui:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfLeft setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui-2"] forState:UIControlStateNormal];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithCustomView:self.buttonOfLeft];
    self.navigationItem.leftBarButtonItem = bar;
    [bar release];
    

    [self handleData];
    
    [self createTableView];
    
}

- (void)handleData {
    
    self.array = @[@"分享", @"清除缓存", @"夜间模式",  @"关于我们"];
    self.arr = @[@"fenxiang1", @"HanCun", @"夜间模式",  @"GuanYuWoMen"];
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"pool"];
    [self.tableView registerClass:[YeJianMoShi class] forCellReuseIdentifier:@"pool2"];
    
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row != 2) {
        
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"pool"];
        
        cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
        cell.imageView.image = [UIImage imageNamed:self.arr[indexPath.row]];

        cell.textLabel.dk_textColorPicker = DKColorWithRGB(0x343434, 0xffffff);
        cell.detailTextLabel.dk_textColorPicker = DKColorWithRGB(0x343434, 0xffffff);
        
        if (indexPath.row == 1) {
            
            /** 获取缓存大小 */
            NSUInteger size = [[SDImageCache sharedImageCache] getSize];
            float cacheSize = size / 1024.0 / 1024.0;
            self.h = cacheSize;
            cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM", self.h];
            
        }
        
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        
        return cell;
        
    } else {
        
        YeJianMoShi *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2"];
        cell.dk_backgroundColorPicker = DKColorWithRGB(0xffffff, 0x343434);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }
  
}

- (void)fanHui:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

/** 点击cell */
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.row == 1) {
 
        NSString *string = [NSString stringWithFormat:@"清除:%.2fM",self.h];
      
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //不作处理
        }];
        
        UIAlertAction *OK = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            //清除缓存
            [[SDImageCache sharedImageCache] clearDisk];
            
            [self.tableView reloadData];
            
        }];
        
        [alert addAction:cancel];
        [alert addAction:OK];
        [self presentViewController:alert animated:YES completion:nil];
        
    } else if (indexPath.row == 3) {
        
        GuanYuWoMen *vc = [[GuanYuWoMen alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        
    } else if (indexPath.row == 0) {
        
        // 1.判断平台是否可用(系统没有集成,用户设置账号).
        if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
            NSLog(@"设置界面设置自己的账号");
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请到设置中填写自己的账号" message:nil preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *actionOk = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                dispatch_after(0.5, dispatch_get_main_queue(), ^{
                    
                    NSURL *url = [NSURL URLWithString:@"prefs:root="];
                    if ([[UIApplication sharedApplication] canOpenURL:url])
                    {
                        [[UIApplication sharedApplication] openURL:url];
                    }
                    
                });
                
            }];
            UIAlertAction *actionCal = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alert addAction:actionOk];
            [alert addAction:actionCal];
            
            [self presentViewController:alert animated:YES completion:^{
                
                
            }];
            
            return;
        }
        
        
        // 2.创建分享控制器.
        SLComposeViewController *composeVc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
        
        // 2.1.添加分享的文字.
            [composeVc setInitialText:@"下载See The World应用, 我们带你看世界!"];
        
      //  [composeVc setInitialText:model.des];
        
        // 2.2.添加分享的图片.
        //    [composeVc addImage:[UIImage imageNamed:@"8bc3dd5c103853439da2910c9213b07ecb808893.jpg"]];
        
      //  [composeVc addImage:imageView.image];
        
        // 3.弹出控制器进行分享.
        [self presentViewController:composeVc animated:YES completion:nil];
        
        // 4.设置监听发送结果.
        composeVc.completionHandler = ^(SLComposeViewControllerResult reulst) {
            if (reulst == SLComposeViewControllerResultDone) {
                NSLog(@"用户发送成功");
            } else {
                NSLog(@"用户发送失败");
            }
        };
        
        
        
    }
    
    

    
}

/** 视图将要出现. */
- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorWithColors([UIColor colorWithRed:43 / 256.f green:115 / 256.f blue:217 / 256.f alpha:1], [UIColor colorWithRed:39 / 256.f green:39 / 256.f blue:39 / 256.f alpha:1]);
    
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"夜间模式"]) {
   
        [DKNightVersionManager nightFalling];
        
    } else {
   
        [DKNightVersionManager dawnComing];
    }
    
    [self.tableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
